package com.san.common.auth.controller;

import com.san.common.auth.dto.ResultDTO;
import com.san.common.auth.dto.UserInfo;
import com.san.common.auth.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * 登录
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Controller
@Slf4j
public class AuthController {

	@Autowired
	UserService userService;

	/**
	 * 页面跳转
	 * @return
	 */
	// @Secured("ROLE_abC")
	//PreAuthorize允许角色以ROLE_开头，也可以不以ROLE_开头，但是配置类不允许以ROLE_开头
	@RequestMapping("/index")
	public String index(Model model,
						HttpServletRequest request){
		Principal userPrincipal = request.getUserPrincipal();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userPrincipal.getName());
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("title", "用户中心");
		return "index";
	}


	/**
	 * 页面跳转
	 * @return
	 */
	@RequestMapping("/toError")
	public String error(){
		return "redirect:error.html";
	}


	/**
	 * 页面跳转
	 * @return
	 */
	@GetMapping("/demo")
	public String demo(Model model,
					   HttpServletRequest request){
		Principal userPrincipal = request.getUserPrincipal();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userPrincipal.getName());
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("title", "用户中心");
		return "demo";
	}

	@ResponseBody
	@GetMapping("/user")
	public ResultDTO getUser(HttpServletRequest request) {
		Principal userPrincipal = request.getUserPrincipal();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userPrincipal.getName());
		return ResultDTO.success("", userInfo.getUserName());
	}
}
